package net.heimeng.bphelper;


import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 弹窗辅助类
 * &#064;ClassName  WindowUtils
 */
public class FloatWindow {
    final static LayoutParams params = new LayoutParams();
    private static final String LOG_TAG = "悬浮窗";
    public static Boolean isShown = false;
    private static View mView = null;
    private static WindowManager mWindowManager;

    private static int HeroNum = 0;
    private static String heros[] = {"东皇太一","","","",""};

    /**
     * 显示弹出框
     */
    public static void showPopupWindow(final Context context) {

        if (isShown) {
            Log.d(LOG_TAG, "正在展示悬浮窗");
            return;
        }
        isShown = true;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.floating_window, null);
        Log.d(LOG_TAG, "弹出悬浮窗");
        // 获取应用的Context
        Context mContext = context.getApplicationContext();
        // 获取WindowManager
        mWindowManager = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        final View popupWindowView = mView.findViewById(R.id.popup_window);// 非透明的内容区域
        mView.setOnTouchListener(new OnTouchListener() {
            //控制退出
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                // 记录按下时的初始位置和触摸坐标
                int x = (int) event.getX();
                int y = (int) event.getY();
                Rect rect = new Rect();
                popupWindowView.getGlobalVisibleRect(rect);
                if (!rect.contains(x, y)) {

                    Log.d(LOG_TAG, "点击悬浮窗外部");
                    hidePopupWindow();
                } else {
                    Log.d(LOG_TAG, "点击悬浮窗内部");
                }

                return false;
            }
        });
        // 类型
        params.type = LayoutParams.TYPE_APPLICATION_OVERLAY;
        // WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
        params.format = PixelFormat.TRANSLUCENT;
        params.width = LayoutParams.MATCH_PARENT;
        params.flags = FLAG_WATCH_OUTSIDE_TOUCH | FLAG_NOT_FOCUSABLE;
        params.height = LayoutParams.MATCH_PARENT;

        params.gravity = Gravity.CENTER;
        Button buttonInFloatWindow = mView.findViewById(R.id.button_catch);
        Button button_close = mView.findViewById(R.id.button_quit);
        Button button_hide = mView.findViewById(R.id.button_hide);
        mWindowManager.addView(mView, params);
        buttonInFloatWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("BUTTON", "clicked");
                // 按钮点击事件处理
                TextView textView = mView.findViewById(R.id.textview_first);
                Toast.makeText(mContext, "悬浮窗按钮被点击，开始截图", Toast.LENGTH_SHORT).show(); // 传递 context 到 requestScreenCapture

                BPService bpService = new BPService(false);
                bpService.blueTeam.addPickedHero(heros[HeroNum++]);
                if(HeroNum>4){
                    HeroNum = 0;
                    bpService.blueTeam.pickedClear();
                }
                textView.setText(bpService.submit());
            }

        });

        button_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hidePopupWindow();
                FloatWindowManager.removeBallView(mContext);

            }
        });

        button_hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hidePopupWindow();
            }
        });

        Log.d(LOG_TAG, "add view");


    }

    /**
     * 隐藏弹出框
     */

    public static void hidePopupWindow() {
        if (isShown && mView != null) {
            Log.d(LOG_TAG, "隐藏悬浮窗");
            mWindowManager.removeView(mView);
            isShown = false;
        }

    }

}
